package com.oig.sys.msg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.oig.common.constants.CommonEnum;
import com.oig.sys.msg.dao.ITbPushChannelDao;
import com.oig.sys.msg.dto.PushChannelDto;
import com.oig.sys.msg.entity.TbPushChannel;
import com.oig.sys.msg.service.ITbPushChannelService;
import com.oig.sys.msg.vo.PushChannelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xulele
 */
@Slf4j
@Service
public class TbPushChannelServiceImpl implements ITbPushChannelService {

    private final ITbPushChannelDao tbPushChannelDao;

    @Autowired
    public TbPushChannelServiceImpl(ITbPushChannelDao tbPushChannelDao) {
        this.tbPushChannelDao = tbPushChannelDao;
    }

    @Transactional
    @Override
    public PushChannelVo addPushChannel(PushChannelDto noticeMsgDto) {
        TbPushChannel tbPushChannel = new TbPushChannel();
        BeanUtil.copyProperties(noticeMsgDto, tbPushChannel, "id");
        tbPushChannel.setStatus(CommonEnum.CommonStatusEnum.OK.getCode());
        tbPushChannel.setLastUpdateName(tbPushChannel.getCreateUserName());
        tbPushChannel.setLastUpdateId(tbPushChannel.getCreateUserId());
        tbPushChannel.setLastUpdateTime(tbPushChannel.getCreateTime());
        tbPushChannelDao.save(tbPushChannel);
        return buildVo(tbPushChannel);
    }

    private PushChannelVo buildVo(TbPushChannel tbPushChannel) {
        PushChannelVo msgVo = new PushChannelVo();
        BeanUtil.copyProperties(tbPushChannel, msgVo);
        return msgVo;
    }

    @Transactional
    @Override
    public PushChannelVo updatePushChannel(PushChannelDto pushChannelDto) {
        TbPushChannel channel = tbPushChannelDao.getChannelById(pushChannelDto.getId());
        BeanUtils.copyProperties(pushChannelDto, channel, "status", "tenantId", "tenantName", "createUserId", "createUserName", "createTime");
        tbPushChannelDao.save(channel);
        return buildVo(channel);
    }

    @Transactional
    @Override
    public void delPushChannel(Integer id) {
        TbPushChannel channel = this.tbPushChannelDao.getChannelById(id);
        channel.setStatus(CommonEnum.CommonStatusEnum.DEL.getCode());
        this.tbPushChannelDao.save(channel);
    }


    @Override
    public PushChannelVo getPushChannel(Integer id) {
        TbPushChannel pushChannel = this.tbPushChannelDao.getChannelById(id);
        if (pushChannel==null) return null;
        return buildVo(pushChannel);
    }

    @Override
    public List<PushChannelVo> listChannel(String supportMsgType, Integer tenantId) {
        List<TbPushChannel> allChannel = tbPushChannelDao.findByStatus(tenantId, supportMsgType, CommonEnum.CommonStatusEnum.OK.getCode());
        List<PushChannelVo> pushChannelVos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(allChannel)) {
            for (TbPushChannel tbPushChannel : allChannel) {
                PushChannelVo pushChannelVo = BeanUtil.copyProperties(tbPushChannel, PushChannelVo.class);
                pushChannelVos.add(pushChannelVo);
            }
        }
        return pushChannelVos;
    }



}
